// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Register from "../views/Register.vue";
import { useStudentStore } from '../store/student'

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/register',
        name: 'Register',
        component: Register
    },
    // 管理员路由
    {
        path: '/admin',
        component: () => import('../views/admin/AdminLayout.vue'),
        meta: { requiresAuth: true, role: 'ADMIN' },
        children: [
            {
                path: '',
                name: 'AdminDashboard',
                component: () => import('../views/admin/Dashboard.vue')
            },
            {
                path: 'dashboard',
                name: 'AdminDashboardRedirect',
                redirect: '/admin'
            },
            {
                path: 'profile',
                name: 'AdminProfile',
                component: () => import('../views/profile/UserProfile.vue')
            },
            {
                path: 'users',
                name: 'UserManagement',
                component: () => import('../views/admin/UserManagement.vue')
            },
            {
                path: 'companies/pending',
                name: 'CompanyReview',
                component: () => import('../views/admin/CompanyReview.vue')
            },
            {
                path: 'statistics',
                name: 'Statistics',
                component: () => import('../views/admin/Statistics.vue')
            },
            {
                path: 'positions',
                name: 'PositionManagement',
                component: () => import('../views/admin/PositionManagement.vue')
            },
            {
                path: 'internships',
                name: 'InternshipManagement',
                component: () => import('../views/admin/InternshipManagement.vue')
            },

        ]
    },
    // 学生路由
    {
        path: '/student',
        component: () => import('../views/student/StudentLayout.vue'),
        meta: { requiresAuth: true, role: 'STUDENT' },
        children: [
            {
                path: '',
                name: 'StudentDashboard',
                component: () => import('../views/student/Dashboard.vue')
            },
            {
                path: 'dashboard',
                name: 'StudentDashboardRedirect',
                redirect: '/student'
            },
            {
                path: 'profile-setup',
                name: 'StudentProfileSetup',
                component: () => import('../views/student/StudentProfileSetup.vue')
            },
            {
                path: 'profile',
                name: 'StudentProfile',
                component: () => import('../views/profile/UserProfile.vue')
            },
            {
                path: 'positions',
                name: 'PositionList',
                component: () => import('../views/student/PositionList.vue')
            },
            {
                path: 'apply/:id',
                name: 'ApplyPosition',
                component: () => import('../views/student/ApplyPosition.vue'),
                props: true
            },
            {
                path: 'applications',
                name: 'ApplicationHistory',
                component: () => import('../views/student/ApplicationHistory.vue')
            },
            {
                path: 'materials',
                name: 'MaterialSubmission',
                component: () => import('../views/student/MaterialSubmission.vue')
            },
            {
                path: 'grades',
                name: 'GradeView',
                component: () => import('../views/student/GradeView.vue')
            }
        ]
    },
    // 教师路由
    {
        path: '/teacher',
        component: () => import('../views/teacher/TeacherLayout.vue'),
        meta: { requiresAuth: true, role: 'TEACHER' },
        children: [
            {
                path: '',
                name: 'TeacherDashboard',
                redirect: '/teacher/dashboard'
            },
            {
                path: 'dashboard',
                name: 'TeacherMainDashboard',
                component: () => import('../views/teacher/Dashboard.vue')
            },
            {
                path: 'profile-setup',
                name: 'TeacherProfileSetup',
                component: () => import('../views/teacher/TeacherProfileSetup.vue')
            },
            {
                path: 'profile',
                name: 'TeacherProfile',
                component: () => import('../views/teacher/UserProfile.vue')
            },
            {
                path: 'students',
                name: 'StudentManagement',
                component: () => import('../views/teacher/StudentManagement.vue')
            },
            {
                path: 'reports',
                name: 'ReportReview',
                component: () => import('../views/teacher/ReportReview.vue')
            },
            {
                path: 'grades',
                name: 'GradeManagement',
                component: () => import('../views/teacher/GradeManagement.vue')
            }
        ]
    },
    // 企业路由
    {
        path: '/company',
        component: () => import('../views/company/EnterpriseLayout.vue'),
        meta: { requiresAuth: true, role: 'COMPANY' },
        children: [
            {
                path: '',
                name: 'CompanyDashboard',
                redirect: '/company/dashboard'
            },
            {
                path: 'dashboard',
                name: 'CompanyMainDashboard',
                component: () => import('../views/company/Dashboard.vue')
            },
            {
                path: 'profile-setup',
                name: 'CompanyProfileSetup',
                component: () => import('../views/company/CompanyProfileSetup.vue')
            },
            {
                path: 'profile-review',
                name: 'CompanyProfileReview',
                component: () => import('../views/company/ProfileReview.vue')
            },
            {
                path: 'profile',
                name: 'CompanyProfile',
                component: () => import('../views/company/Profile.vue')
            },
            {
                path: 'positions',
                name: 'CompanyPositionManagement',
                component: () => import('../views/company/PositionManagement.vue')
            },
            {
                path: 'applications',
                name: 'CompanyApplicationManagement',
                component: () => import('../views/company/ApplicationManagement.vue')
            },
            {
                path: 'evaluations',
                name: 'CompanyEvaluationManagement',
                component: () => import('../views/company/EvaluationManagement.vue')
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
    console.log('路由跳转:', from.path, '->', to.path)
    const token = localStorage.getItem('token')
    const userRole = localStorage.getItem('userRole')

    if (to.meta.requiresAuth && !token) {
        console.log('未认证，重定向到登录页')
        next({ name: 'Login' })
    } else if (to.meta.role && to.meta.role !== userRole) {
        console.log('角色权限不足，重定向到登录页')
        next({ name: 'Login' })
    } else {
        console.log('路由跳转成功')
        next()
    }
})

export default router
